package com.ycsj.goldmedalnewconcept.activity;

import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.utils.UiHelper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;




/**
 * 创建日期：2017/11/10  17:43
 * 描述:编辑名称,口号,目标通用界面
 * 作者:张伟
 */

@SuppressLint("NewApi")
public class EditStudentActivity extends Activity implements View.OnClickListener {

    private LinearLayout llBack;

    private TextView tvSave, tvTitle;

    private EditText etInput;

    private String flag;

    public static final int NAMERESULT = 0X110;
    public static final int NICKNAMERESULT = 0X120;
    public static final int SCHOOLRESULT = 0X130;
    public static final int PAYMENTRESULT = 0X140;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_student);

        //设置状态栏
        setBar();

        //初始化View
        initView();

        //获取已经使用的小组图标的标记集合
        getData();

        //设置监听
        initListener();

    }

    /**
     * 初始化View的操作
     */
    private void initView() {
        llBack = (LinearLayout) findViewById(R.id.ll_back);
        tvSave = (TextView) findViewById(R.id.tv_save);
        etInput = (EditText) findViewById(R.id.et_edit);
        tvTitle = (TextView) findViewById(R.id.tv_title);
    }


    /**
     * 获取已经使用的小组图标
     */
    public void getData() {
        flag = getIntent().getStringExtra("edit");
        if ("name".equals(flag)) {
            tvTitle.setText("姓名");
            etInput.setHint("请输入学生名称");
        } else if ("nickname".equals(flag)) {
            tvTitle.setText("昵称");
            etInput.setHint("请输入学生昵称");
        } else if ("school".equals(flag)) {
            tvTitle.setText("学校");
            etInput.setHint("请输入学校名称");
        } else if ("payment".equals(flag)) {
            tvTitle.setText("缴费状态");
            etInput.setHint("请输入缴费状态");
        }
    }

    /**
     * 设计监听的方法
     */
    private void initListener() {
        llBack.setOnClickListener(this);
        tvSave.setOnClickListener(this);
    }


    /**
     * 页面控件的点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                //返回
                finish();
                break;
            case R.id.tv_save:
                //保存
                String content = etInput.getText().toString().trim();
                Intent intent = new Intent();
                intent.putExtra("data", content);
                if ("name".equals(flag)) {
                    //修改学生姓名
                    if (!TextUtils.isEmpty(content)) {
                        setResult(NAMERESULT, intent);
                        finish();
                    } else {
                        UiHelper.toast(this, "请先输入学生姓名");
                        return;
                    }
                } else if ("nickname".equals(flag)) {
                    //修改学生昵称
                    if (!TextUtils.isEmpty(content)) {
                        setResult(NICKNAMERESULT, intent);
                        finish();
                    } else {
                        UiHelper.toast(this, "请先输入学生昵称");
                        return;
                    }
                } else if ("school".equals(flag)) {
                    //修改学生昵称
                    if (!TextUtils.isEmpty(content)) {
                        setResult(SCHOOLRESULT, intent);
                        finish();
                    } else {
                        UiHelper.toast(this, "请先输入学校名称");
                        return;
                    }
                } else if ("payment".equals(flag)) {
                    //修改缴费状态
                    if (!TextUtils.isEmpty(content)) {
                        setResult(PAYMENTRESULT, intent);
                        finish();
                    } else {
                        UiHelper.toast(this, "请先输入缴费状态");
                        return;
                    }
                }
        }
    }

    /**
     * 设置沉浸状态栏的方法,需要在setContentView()之后调用
     */
    private void setBar() {
        //兼容5.0+，状态栏设置透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else
            //为了沉浸式状态栏 4.4以上才有
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
    }
}
